local skel = fk.CreateSkill({
    name = "sj9__mingyifu",
  })
  
  Fk:loadTranslationTable{
    ["sj9__mingyifu"] = "命易覆",
    [":sj9__mingyifu"] = "回合开始时，你可以令一名角色将手牌调整至全场最多/最少，则其下次造成或受到伤害后，其将之调整至全场最少/最多。",
  
    ["#sj9__mingyifu"] = "命易覆：你可以令一名角色将手牌调整至全场最多或最少",
    ["#sj9__mingyifu_min"] = "最少",
    ["#sj9__mingyifu_max"] = "最多",
    ["@sj9__mingyifu"] = "命易覆",
  
    ["$sj9__mingyifu1"] = "",
    ["$sj9__mingyifu2"] = "",
  }


  skel:addEffect(fk.TurnStart, {
    can_trigger = function(self, event, target, player, data)
      return player:hasSkill(skel.name) and player == target
    end,
    on_cost = function (self, event, target, player, data)
      local room = player.room
      local max = player:getHandcardNum()
      local min = max
      for _, t in ipairs(room:getOtherPlayers(player)) do
        local n = t:getHandcardNum()
        if n > max then max = n end
        if n < min then min = n end
      end
      if max == min then return end
      local s, ret = room:askToUseActiveSkill(player,{
        skill_name = "#sj9__mingyifu_choose",
        prompt = "#sj9__mingyifu",
        extra_data = {
          extra_data = {
          min = min,
          max = max,
        }},
      })
      if s and ret and ret.targets and ret.interaction then
        event:setCostData(self, {tos = ret.targets, i = ret.interaction})
        return true
      end
    end,
    on_use = function(self, event, target, player, data)
      local room = player.room
      local t = event:getCostData(self).tos[1]
      local n = tonumber(event:getCostData(self).i)
      local mark = "#sj9__mingyifu_max"
      if t:getHandcardNum() > n then
        room:askToDiscard(t,{
          min_num = t:getHandcardNum() - n,
          max_num = t:getHandcardNum() - n,
          include_equip = false,
          skill_name = skel.name,
          cancelable = false,
        })
      else
        room:drawCards(t,n - t:getHandcardNum(),skel.name)
        mark = "#sj9__mingyifu_min"
      end
      room:setPlayerMark(t,"@sj9__mingyifu",mark)
    end,
  })
  local mingyifu_spec = {
    is_delay_effect = true,
    can_trigger = function(self, event, target, player, data)
      return target and player:hasSkill(skel.name) and target:getMark("@sj9__mingyifu") ~= 0
    end,
    on_cost = function (self, event, target, player, data)
      local room = player.room
      local mark = target:getMark("@sj9__mingyifu")
      room:setPlayerMark(target,"@sj9__mingyifu",0)
      local max = player:getHandcardNum()
      local min = max
      for _, t in ipairs(room:getOtherPlayers(player)) do
        local n = t:getHandcardNum()
        if n > max then max = n end
        if n < min then min = n end
      end
      if max == min then return end
      local n = (mark == "#sj9__mingyifu_min") and min or max
      event:setCostData(self, {n = n})
      return n ~= target:getHandcardNum()
    end,
    on_use = function(self, event, target, player, data)
      local room = player.room
      local n = event:getCostData(self).n
      if target:getHandcardNum() > n then
        room:askToDiscard(target,{
          min_num = target:getHandcardNum() - n,
          max_num = target:getHandcardNum() - n,
          include_equip = false,
          skill_name = skel.name,
          cancelable = false,
        })
      else
        room:drawCards(target,n - target:getHandcardNum(),skel.name)
      end
    end,
  }
  skel:addEffect(fk.Damage, mingyifu_spec)
  skel:addEffect(fk.Damaged, mingyifu_spec)

return skel